import { Injectable } from "@angular/core";
import { BaseHttpService } from "../../../shared/services/baseHttp.service";
import { Observable } from "rxjs";
import { HttpClient, HttpParams } from "@angular/common/http";
import { SpotlightModel, SpotlightCategory, SpotlightAvailability } from "./model/spotlights.model";

@Injectable()
export class SpotlightsService extends BaseHttpService {
    constructor(private http: HttpClient) {
        super()
    }

    public getSpotlightsList(term: string, category: SpotlightCategory, availability: SpotlightAvailability, skip: number, take: number): Observable<SpotlightModel[]> {
        let listOptions: any = {
            skip: skip,
            take: take,
            orderBy: ['DateCreated'],
            orderAscending: false,
            includeAllFields: true
        };
        let params = new HttpParams();
        params = params.set('term', term)
            .set('category', SpotlightCategory[category])
            .set('isPublished', false.toString())
            .set('availableFilter', availability.toString())
            .set('accountId', '')
            .set('fromAdmin', true.toString());
        params = this.urlEncodeObject(listOptions, params);
        return this.http.get<SpotlightModel[]>(this.serverUrl + '/newsserv/api/v1/news', { params: params });
    }

    public getSpotlightsCount(term: string, category: SpotlightCategory, availability: SpotlightAvailability): Observable<number> {
        let listOptions: any = {
            getCount: true
        };
        let params = new HttpParams();
        params = params.set('term', term)
            .set('category', SpotlightCategory[category])
            .set('isPublished', false.toString())
            .set('availableFilter', availability.toString())
            .set('accountId', '')
            .set('fromAdmin', true.toString());
        params = this.urlEncodeObject(listOptions, params);
        return this.http.get<number>(this.serverUrl + '/newsserv/api/v1/news', { params: params });
    }

    public publishSpotlights(ids: string[]): Observable<void> {
        return this.http.patch<void>(this.serverUrl + '/newsserv/api/v1/news', { publishNewsItems: ids });
    }

    public deleteSpotlights(ids: string[]): Observable<void> {
        return this.http.patch<void>(this.serverUrl + '/newsserv/api/v1/news', { deleteItems: ids });
    }

    public getSpotlight(id: string): Observable<SpotlightModel> {
        let params = new HttpParams();
        params = params.set('accountId', '')
            .set('fromAdmin', true.toString())
        return this.http.get<SpotlightModel>(this.serverUrl + '/newsserv/api/v1/news/' + id, { params: params });
    }

    public publishSpotlight(spotlight: SpotlightModel): Observable<SpotlightModel> {        
        return this.http.post<SpotlightModel>(this.serverUrl + '/newsserv/api/v1/news', spotlight)
    }

    public saveSpotlight(spotlight: SpotlightModel): Observable<SpotlightModel> {        
        return this.http.put<SpotlightModel>(this.serverUrl + '/newsserv/api/v1/news', spotlight)
    }
}